#import "ZLTextDemoViewController.h"
#import "ZLLabel.h"

@interface ZLTextDemoViewController ()

@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation ZLTextDemoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 创建滚动视图
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.scrollView];
    
    // 展示各种使用场景
    [self setupBasicExample];
    [self setupMultipleLinksExample];
    [self setupCustomStyleExample];
    [self setupRichTextExample];
    [self setupPhoneLinkExample];
}

#pragma mark - Examples

// 基础示例：简单的链接文本
- (void)setupBasicExample {
    ZLLabel *label = [[ZLLabel alloc] initWithFrame:CGRectMake(20, 20, self.view.bounds.size.width - 40, 60)];
    
    NSString *text = @"这是一个基础示例，点击这里查看详情。";
    label.attributedText = [[NSAttributedString alloc] initWithString:text];
    
    // 添加可点击文本
    [label addLinkForText:@"点击这里" withAction:^{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"
                                                                     message:@"你点击了链接"
                                                              preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
        [self presentViewController:alert animated:YES completion:nil];
    }];
    
    [self.scrollView addSubview:label];
}

// 多链接示例：一段文本中包含多个可点击区域
- (void)setupMultipleLinksExample {
    ZLLabel *label = [[ZLLabel alloc] initWithFrame:CGRectMake(20, 100, self.view.bounds.size.width - 40, 100)];
    
    NSString *text = @"这段文本包含多个链接，你可以点击链接1，也可以点击链接2，或者点击链接3。";
    label.attributedText = [[NSAttributedString alloc] initWithString:text];
    
    // 添加多个可点击文本
    [label addLinkForText:@"链接1" withAction:^{
        NSLog(@"点击了链接1");
    }];
    
    [label addLinkForText:@"链接2" withAction:^{
        NSLog(@"点击了链接2");
    }];
    
    [label addLinkForText:@"链接3" withAction:^{
        NSLog(@"点击了链接3");
    }];
    
    [self.scrollView addSubview:label];
}

// 自定义样式示例：自定义高亮文本的样式
- (void)setupCustomStyleExample {
    ZLLabel *label = [[ZLLabel alloc] initWithFrame:CGRectMake(20, 220, self.view.bounds.size.width - 40, 80)];
    
    NSString *text = @"这个示例展示了自定义样式的链接文本。";
    label.attributedText = [[NSAttributedString alloc] initWithString:text];
    
    // 创建自定义样式的高亮文本
    NSRange range = [text rangeOfString:@"自定义样式"];
    ZLTextHighlight *highlight = [ZLTextHighlight highlightWithRange:range];
    highlight.normalColor = [UIColor redColor];
    highlight.highlightColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.3];
    highlight.showUnderline = NO;
    highlight.tapAction = ^{
        NSLog(@"点击了自定义样式文本");
    };
    
    [label addHighlight:highlight];
    
    [self.scrollView addSubview:label];
}

// 富文本示例：结合富文本和可点击文本
- (void)setupRichTextExample {
    ZLLabel *label = [[ZLLabel alloc] initWithFrame:CGRectMake(20, 320, self.view.bounds.size.width - 40, 100)];
    
    // 创建富文本
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:@"这是一段富文本示例，包含粗体文本和可点击的链接。"];
    
    // 设置粗体
    NSRange boldRange = [attributedText.string rangeOfString:@"粗体文本"];
    [attributedText addAttribute:NSFontAttributeName
                         value:[UIFont boldSystemFontOfSize:17]
                         range:boldRange];
    
    label.attributedText = attributedText;
    
    // 添加可点击文本
    [label addLinkForText:@"可点击的链接" withAction:^{
        NSLog(@"点击了富文本中的链接");
    }];
    
    [self.scrollView addSubview:label];
}

// 电话链接示例：点击拨打电话
- (void)setupPhoneLinkExample {
    ZLLabel *label = [[ZLLabel alloc] initWithFrame:CGRectMake(20, 440, self.view.bounds.size.width - 40, 60)];
    
    NSString *text = @"如需帮助，请拨打客服电话：400-123-4567";
    label.attributedText = [[NSAttributedString alloc] initWithString:text];
    
    // 添加电话链接
    [label addLinkForText:@"400-123-4567" withAction:^{
        NSString *phoneNumber = @"4001234567";
        NSString *telUrl = [NSString stringWithFormat:@"tel://%@", phoneNumber];
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:telUrl]]) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:telUrl] options:@{} completionHandler:nil];
        }
    }];
    
    [self.scrollView addSubview:label];
    
    // 设置滚动视图的内容大小
    self.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, 520);
}

@end 
